//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcIconPicker.h"

@implementation _smcTextLabel
/* super small subclass for mouse events */
- (BOOL) ignoresMouseEvents 
{
	// superclass does! this one doesn't
	return NO;
}
@end

@implementation _smcBox
/* super small subclass for nicer box */
- (void) drawRect: (CGRect) rect
{
	CGContextSetFillColorWithColor(UICurrentContext(), [UIView colorWithRed: 0.95f green: 0.95f blue: 0.95f alpha: 1.0f]);
	[[UIBezierPath roundedRectBezierPath: rect withRoundedCorners: 15 withCornerRadius: 10.0f] fill];	
}
@end

@implementation smcIconPicker

#define ICON_SIZE 40.0f

#pragma mark Standard

- (void) dealloc
{
	[_box release];
	[_iconNames release];
	[_scrollView release];
	[super dealloc];
}

- (id) initWithFrame: (CGRect) rect
{
	self = [super initWithFrame: rect];
	if (self != nil)
	{
		_selectedOne = -1;
		// read in the icon names
		_iconNames = [[NSMutableArray arrayWithCapacity: 100] retain];
		for (int i = 303; i <= 403; i++) 
			if ([UIImage applicationImageNamed: [NSString stringWithFormat: @"%i.png", i]] != nil)
				[_iconNames addObject: [NSString stringWithFormat: @"%i", i]];



		// a scroll view
		_scrollView = [[UIScroller alloc] initWithFrame: CGRectMake(5.0f, 5.0f, rect.size.width - 10.0f, ICON_SIZE + 5.0f)];
		[_scrollView setContentSize: CGSizeMake (([_iconNames count]+1)*ICON_SIZE + 10.0f, ICON_SIZE)];
		[_scrollView setShowScrollerIndicators: NO];
		[_scrollView setAllowsRubberBanding: YES];
		[_scrollView setRubberBand: 0.0f forEdges: 10];
		[_scrollView setRubberBand: 0.5f forEdges: 5];
		

		// in a nice box
		_box = [[_smcBox alloc] initWithFrame: CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height)];
		[_box addSubview: _scrollView];
		// add all images 
		UIImageView *tmp = nil;
		for (int i = 0; i < [_iconNames count]; i++)
		{
			tmp = [[[UIImageView alloc] initWithFrame: CGRectMake (i*ICON_SIZE, 5.0f, ICON_SIZE, ICON_SIZE)] autorelease];
			[tmp setImage: [UIImage applicationImageNamed: 
				[NSString stringWithFormat: @"%@.png", [_iconNames objectAtIndex: i]]]];
			[tmp setTapDelegate: self];
			[tmp setTag: i];
			[_scrollView addSubview: tmp];
		}
		
		// the remove label
		_smcTextLabel *label = [[_smcTextLabel alloc] initWithFrame: 
			CGRectMake([_iconNames count]*ICON_SIZE, 0.0f, ICON_SIZE + 10.0f, ICON_SIZE)];
		[label setBackgroundColor:  [UIView colorWithRed: 0.95f green: 0.95f blue: 0.95f alpha: 1.0f]];
		[label setText: @"none"];
		[label setTag: -1];
		[label setCentersHorizontally: YES];
		[label setTapDelegate: self];
		[_scrollView addSubview: label];
		
		[self addSubview: _box];
	}
	
	return self;
}

#pragma mark image views tap delegate

- (BOOL)view:(id)view handleTapWithCount: (int) count event: (struct __GSEvent *) event
{
	_selectedOne = [view tag];
	// call delegate
	if ([[self delegate] respondsToSelector: @selector(iconSelectedInPicker:)])
		[[self delegate] performSelector: @selector(iconSelectedInPicker:) withObject: self];
}

#pragma mark set & get & action

- (void) resetIcons
{
	_selectedOne = -1;
}

- (int) selectedIconID
{
	if (_selectedOne != -1)
		return [[_iconNames objectAtIndex: _selectedOne] intValue];
	else
		return 0;
}

- (UIImage *) selectedIcon
{
	if (_selectedOne != -1)
		return [[[_scrollView subviews] objectAtIndex: _selectedOne] image];
	else
		return [UIImage applicationImageNamed: @"plus.png"];
}


@end
